package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.common.Common;
import com.yc.jpyy.mobile.dao.TeacherDao;
import com.yc.jpyy.mobile.service.TeacherService;

@Controller
@RequestMapping(value = "/teacherlogin")
public class TeacherLoginController extends BaseController {
	@Autowired
	private TeacherService teacherService;

	@Autowired
	private TeacherDao teacherDao;
	private static Logger logger = LoggerFactory.getLogger(TeacherLoginController.class);

	/**
	 * 系统登录
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/teachLogin", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String teachLogin(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 身份证号
			String IdCardNo = request.getParameter("IdCardNo");
			// 密码
			String password = request.getParameter("password");

			if (IdCardNo != null && !IdCardNo.isEmpty() && password != null
					&& !password.isEmpty()) {
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("idcardno", IdCardNo);
				valMap.put("password", Common.genMDInfo(password));
				valMap.put("imgUrl", Config.getImageAddress());
				// 教练登陆
				response = teacherService.teacherLogin(valMap, response);
			} else {
				response.setErrorcode(1);
				response.setMessage("身份证号或密码不能为空...");
			}
		} catch (Exception e) {
			logger.info("teachLogin:" + e.toString());
			response.setReturnMsg(1, "登录失败！");
		} finally {
			return response.toJSON();
		}
	}

}
